﻿
Namespace htmlUtils
    Public Module _htmlUtils
#If Not (PocketPC OrElse Smartphone) Then
        Public Function escape(ByRef s As String) As String
            s = urlUtils.htmlencode(s)
            Return s
        End Function

        Public Function escapeBack(ByRef s As String) As String
            s = urlUtils.htmldecode(s)
            Return s
        End Function

        Public Function unescape(ByRef s As String) As String
            Return escapeBack(s)
        End Function
#Else
        Public escapePairs As vector(Of pair(Of String, String)) = Nothing
        Public escapeBackPairs As vector(Of pair(Of String, String)) = Nothing
        Public escapeMappings(,) As String = {{character.quoteMark, "&quot;"} _
                                            , {character.andMark, "&amp;"} _
                                            , {character.leftAngleBracket, "&lt;"} _
                                            , {character.rightAngleBracket, "&gt;"} _
                                            , {character.blank, "&nbsp;"}}
#If CANNOT_DETERMINE Then
                                            , {"?", "&iexcl;"} _
                                            , {"á", "&Aacute;"} _
                                            , {"á", "&aacute;"} _
                                            , {"￠", "&cent;"} _
                                            , {"?", "&circ;"} _
                                            , {"a", "&acirc;"} _
                                            , {"￡", "&pound;"} _
                                            , {"?", "&Atilde;"} _
                                            , {"?", "&atilde;"} _
                                            , {"¤", "&curren;"} _
                                            , {"?", "&Auml"} _
                                            , {"?", "&auml;"} _
                                            , {"￥", "&yen;"} _
                                            , {"?", "&ring;"} _
                                            , {"?", "&aring;"} _
                                            , {"|", "&brvbar;"} _
                                            , {"?", "&AElig;"} _
                                            , {"?", "&aelig;"} _
                                            , {"§", "&sect;"} _
                                            , {"?", "&Ccedil;"} _
                                            , {"?", "&ccedil;"} _
                                            , {"¨", "&uml;"} _
                                            , {"è", "&Egrave;"} _
                                            , {"è", "&egrave;"} _
                                            , {"?", "&copy;"} _
                                            , {"é", "&Eacute;"} _
                                            , {"é", "&eacute;"} _
                                            , {"a", "&ordf;"} _
                                            , {"ê", "&Ecirc;"} _
                                            , {"ê", "&ecirc;"} _
                                            , {"?", "&laquo;"} _
                                            , {"?", "&Euml;"} _
                                            , {"?", "&euml;"} _
                                            , {"?", "&not;"} _
                                            , {"ì", "&Igrave;"} _
                                            , {"ì", "&igrave;"} _
                                            , {"\x7f", "&shy;"} _
                                            , {"í", "&Iacute;"} _
                                            , {"í", "&iacute;"} _
                                            , {"?", "&reg;"} _
                                            , {"?", "&Icirc;"} _
                                            , {"?", "&icirc;"} _
                                            , {"ˉ", "&macr;"} _
                                            , {"?", "&Iuml;"} _
                                            , {"?", "&iuml;"} _
                                            , {"°", "&deg;"} _
                                            , {"D", "&ETH;"} _
                                            , {"e", "&ieth;"} _
                                            , {"±", "&plusmn;"} _
                                            , {"?", "&Ntilde;"} _
                                            , {"?", "&ntilde;"} _
                                            , {"2", "&sup2;"} _
                                            , {"ò", "&Ograve;"} _
                                            , {"ò", "&ograve;"} _
                                            , {"3", "&sup3;"} _
                                            , {"ó", "&Oacute;"} _
                                            , {"ó", "&oacute;"} _
                                            , {"′", "&acute;"} _
                                            , {"?", "&Ocirc;"} _
                                            , {"?", "&ocirc;"} _
                                            , {"μ", "&micro;"} _
                                            , {"?", "&Otilde;"} _
                                            , {"?", "&otilde;"} _
                                            , {"?", "&para;"} _
                                            , {"?", "&Ouml;"} _
                                            , {"?", "&ouml;"} _
                                            , {"·", "&middot;"} _
                                            , {"÷", "&divide;"} _
                                            , {"?", "&cedil;"} _
                                            , {"?", "&Oslash;"} _
                                            , {"?", "&oslash;"} _
                                            , {"1", "&sup1;"} _
                                            , {"ù", "&Ugrave;"} _
                                            , {"ù", "&ugrave;"} _
                                            , {"o", "&ordm;"} _
                                            , {"ú", "&Uacute;"} _
                                            , {"ú", "&uacute;"} _
                                            , {"?", "&raquo;"} _
                                            , {"?", "&Ucirc;"} _
                                            , {"?", "&ucirc;"} _
                                            , {"?", "&frac14;"} _
                                            , {"ü", "&Uuml;"} _
                                            , {"ü", "&uuml;"} _
                                            , {"?", "&frac12;"} _
                                            , {"Y", "&Yacute;"} _
                                            , {"y", "&yacute;"} _
                                            , {"?", "&frac34;"} _
                                            , {"T", "&THORN;"} _
                                            , {"t", "&thorn;"} _
                                            , {"?", "&iquest;"} _
                                            , {"?", "&szlig;"} _
                                            , {"?", "&yuml;"} _
                                            , {"à", "&Agrave;"} _
                                            , {"à", "&agrave;"}}
#End If

        Public Sub initial()
            escapePairs = New vector(Of pair(Of String, String))
            escapeBackPairs = New vector(Of pair(Of String, String))
            Dim i As Int32
            For i = 0 To escapeMappings.GetLength(0) - 1
                escapePairs.push_back(make_pair(escapeMappings(i, 0), escapeMappings(i, 1)))
                escapeBackPairs.push_back(make_pair(escapeMappings(i, 1), escapeMappings(i, 0)))
            Next
        End Sub

        Public Function escape(ByRef s As String) As String
            If escapePairs Is Nothing Then
                initial()
            End If
            strrplc2(s, escapePairs, False)
            Return s
        End Function

        Public Function escapeBack(ByRef s As String) As String
            If escapeBackPairs Is Nothing Then
                initial()
            End If
            strrplc2(s, escapeBackPairs, False)
            Return s
        End Function

        Public Function unescape(ByRef s As String) As String
            Return escapeBack(s)
        End Function
#End If
    End Module
End Namespace
